<template>
  <el-backtop :right="100" :bottom="100"/>
  <Header></Header>
  <div style="text-align: center;margin: 0 auto;font-size: 40px" v-if="!data.isLoggedIn">暂无此商品</div>
  <div style="display: flex;width: 85%; height: 100%; margin: 30px auto;  ">
    <div style=" flex: 1;margin-left: 30px;">
      <div>
        <!-- 根据 product 显示不同的内容 -->
        <div class="s_spring">
          <div v-for="item in data.seasonsData" style="overflow: hidden;" :key="item" class="product-item">
            <a :href="'/product/' + item.flowerId">
              <div class="img-wrapper">
                <img :src="item.imageUrl" alt="商品图片">
              </div>
              <div class="info-wrapper">
                <p class="name">{{ item.flowerName }}</p>
                <p class="name" style="color: #a87567;font-size: 12px">{{ item.groupName }}</p>
                <p class="price">价格：￥{{ item.price }}</p>
                <p class="description">{{ item.description }}</p>
                <div class="button-container">
                  <el-button color="#7a80ee" :dark="isDark" plain  @click="addToCart(item)">
                    <ShoppingCart/>
                    购买
                  </el-button>
                </div>
              </div>
            </a>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>

<script setup>
// 图标
import {ShoppingCart, ShoppingTrolley} from '@element-plus/icons-vue'
import {ref, watch, computed, reactive} from 'vue';
import Header from "@/components/Header.vue";
import Sidebar from "@/components/Sidebar.vue";
import {useRoute} from 'vue-router';
import request from "@/utils/request";

const route = useRoute();
// 我没搞懂下面这行代码有什么用，删了好像也能用

const data = reactive({
  categories: [],
  seasonsData: [],
  isLoggedIn:false,
  aa: route.query.product,
})

const storedFlowers = localStorage.getItem('search');
if (storedFlowers) {
  try {
    // 将字符串转换为 JSON 对象
    data.seasonsData = JSON.parse(storedFlowers);
  } catch (error) {
    console.error('Error parsing JSON from localStorage:', error);
    // 可以设置一个默认值或者进行其他错误处理
    data.seasonsData = [];
  }
} else {
  // 如果没有数据，可以设置一个默认值
  data.seasonsData = [];
}

console.log(data.seasonsData.length)


const si = () => {
  window.location.reload();
}

watch(() => route.query.product, (newProduct) => {
  product.value = decodeURIComponent(newProduct || '');
  // console.log(product.value)
});

const addToCart = (product) => {
  console.log('添加到购物车:', product);
};

if(data.seasonsData.length===0){
  data.isLoggedIn=false
}else{
  data.isLoggedIn=true
}
</script>


<style scoped>
.example-pagination-block + .example-pagination-block {
  margin-top: 10px;
}
.example-pagination-block .example-demonstration {
  margin-bottom: 16px;
}
.el-link:hover{
  background-color: #f8e1d9;
  color: #0e0c0c;
}

.s_spring {  /* 内容样式 */
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  margin: 0 auto;
  max-width: 1200px;
}

.product-item {
  width: 30%;
  height: 297px;
  margin-bottom: 20px;
  margin-right: 20px;
  box-sizing: border-box; /* 包括边框和内边距在内计算宽度 */
  padding: 10px;
  border: 2px solid #eeeeee;
  position: relative;
}

.img-wrapper {
  width: 100%;
  height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  margin-top: 15px;
  margin-bottom: 30px;
}

.img-wrapper img {
  width: 150px;
  max-width: 100%;
  max-height: 100%;
  object-fit: cover;
  border-radius: 5px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.info-wrapper {
  margin-top: 10px;
}
.name{
  font-weight: bold;
  color: #0e0c0c;
}
.name, .price, .description {
  margin: 5px 10px;
  font-size: 14px;
}

.price {
  color: #f2594b;
  font-size: 13px;
}

.description {
  text-overflow: ellipsis;
  font-size: 14px;
  color: #bebebe;
  width: 100%;
}
.button-container {
  position: absolute;
  bottom: 26px;
  right: 20px;

}

a{
  text-decoration: none;
}
.product-item:hover {
  border-color: #f2594b;
}
.circle {
  margin-top: 10px;
  margin-right: 5px;
  width: 30px;
  height: 30px;
  background-color: #ff6347;
  border-radius: 50%;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  line-height: 180%;
  color: #FFFFFF;
  text-align: center;
}
</style>
